# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=rnp
pkgver=0.17.1
pkgrel=1
pkgdesc="High performance C++ OpenPGP library used by Mozilla Thunderbird"
url="https://www.rnpgp.org/"
arch="all"
license="BSD-2-Clause"
checkdepends="gnupg gtest-dev"
makedepends="
	asciidoctor
	botan3-dev
	bzip2-dev
	cmake
	json-c-dev
	sexpp-dev
	zlib-dev
	"
subpackages="$pkgname-dev $pkgname-doc librnp"
options="chmod-clean"
source="https://github.com/rnpgp/rnp/archive/v$pkgver/rnp-$pkgver.tar.gz
	"

case "$CARCH" in
	# FIXME: one test fails
	s390x) options="!check";;
esac

build() {
	# Avoid polluting /tmp.
	export TEMP="$builddir/.tmp"
	mkdir -p "$TEMP"

	local crossopts=
	if [ "$CBUILD" != "$CHOST" ]; then
		crossopts="-DCMAKE_SYSTEM_NAME=Linux -DCMAKE_HOST_SYSTEM_NAME=Linux"
	fi
	# CMAKE_INTERPROCEDURAL_OPTIMIZATION - enable LTO
	cmake -B build \
		-DCMAKE_BUILD_TYPE=MinSizeRel \
		-DCMAKE_INTERPROCEDURAL_OPTIMIZATION=ON \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=lib \
		-DCMAKE_VERBOSE_MAKEFILE=ON \
		-DBUILD_SHARED_LIBS=ON \
		-DBUILD_TESTING="$(want_check && echo ON || echo OFF)" \
		-DDOWNLOAD_GTEST=OFF \
		-DDOWNLOAD_RUBYRNP=OFF \
		-DGIT_EXECUTABLE=/bin/false \
		-DSYSTEM_LIBSEXPP=ON \
		$crossopts .
	cmake --build build
}

check() {
	# cli_tests-Encryption - fails on x86_64, but passes on all others (probably broken test)
	# cli_tests-Misc - probably incompatibility with recent gnupg
	cd build
	CTEST_OUTPUT_ON_FAILURE=TRUE ctest -j1 \
		--exclude-regex '(cli_tests-Encryption|cli_tests-Misc|test_ffi_set_log_fd)$'
}

package() {
	pkgdesc="$pkgdesc (CLI tools)"

	DESTDIR="$pkgdir" cmake --install build
}

librnp() {
	default_libs
}

sha512sums="
0224f79e4dd4b72ddd9c191218327da5485ed1d81321cc3a167cfeea68c00e47cec9151a4177e65e5560039345db3a52e41a093adfdf833f57e867bc559494a5  rnp-0.17.1.tar.gz
"
